package com.tbi.graphcolouring.testers;

import java.util.Arrays;

public class CrossTest {
	public static void main(String[] args) {
		int[] a = {1,1,1,1,1,1,1,1,1,1,1};
		int[] b = {2,2,2,2,2,2,2,2,2,2,2};
		
		int[] aCopy = Arrays.copyOf(a, a.length);
		int[] bCopy = Arrays.copyOf(b, b.length);
		
		cross(aCopy, bCopy, 1);
		
		System.out.println("Old arrays");
		System.out.println(Arrays.toString(a));
		System.out.println(Arrays.toString(b));
		System.out.println("New, crossed arrays");
		System.out.println(Arrays.toString(aCopy));
		System.out.println(Arrays.toString(bCopy));
	}
	
	private static void cross(int[] a, int[] b, int numberOfCrossPoints) {
		if(a.length != b.length){
			throw new IllegalArgumentException();
		}
		
		int gapBetweenCrossPoints = a.length / (numberOfCrossPoints+1);
		boolean swap = true; //first piece of chromosome does not swap. This flag changes on the beggining of loop.
		int crossPointCounter = -1; //first loop call increments this, so I set it to -1
		
		for (int i = 0; i < a.length; i++) {
			if(i%gapBetweenCrossPoints==0 && crossPointCounter != numberOfCrossPoints){
				swap = !swap;
				crossPointCounter++;
			}
			if(swap){
				int temp = a[i];
				a[i] = b[i];
				b[i] = temp;
			}
		}
	}
}
